/*
 * @Author: your name
 * @Date: 2024-05-29 11:33:00
 * @LastEditTime: 2024-05-29 11:36:20
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\134.加油站.cpp
 */
/*
 * @lc app=leetcode.cn id=134 lang=cpp
 *
 * [134] 加油站
 */

// @lc code=start
class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int totalSum = 0;
        int curSum = 0;
        int start_index = 0;
        for (int i = 0; i < gas.size(); i++)
        {
            totalSum += (gas[i]-cost[i]);
            curSum += (gas[i]-cost[i]);
            if(curSum < 0){
                curSum = 0;
                start_index = i+1;
            }
        }
        if(totalSum < 0){
            return -1;
        }
        return start_index;
        
    }
};
// @lc code=end

